package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.NewsContent;
import cn.wolfcode.trip.base.domain.NewsPage;
import cn.wolfcode.trip.base.query.NewsPageQueryObject;
import cn.wolfcode.trip.base.service.INewsPageService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * 日报控制器
 */
@Controller
@RequestMapping("/newsPage")
public class NewsPageController {
    @Autowired
    private INewsPageService newsPageService;

    /**
     * 列表页面
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(@ModelAttribute("qo") NewsPageQueryObject qo, Model model){
        qo.setOrderBy("n.releaseTime desc");
        PageInfo pageInfo = newsPageService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "/newsPage/list";
    }

    /**
     * 新增或修改
     * @param newsPage
     * @param file
     * @return
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(NewsPage newsPage, MultipartFile file){
        if (file != null && file.getSize()>0) {
            String url = UploadUtil.uploadQiniu(file);
            newsPage.setCoverUrl(url);
        }
        newsPageService.saveOrUpdate(newsPage);
        return new JSONResult();
    }

    /**
     * 查询日报的文本内容
     * @param newsId
     * @return
     */
    @RequestMapping("/newsContent")
    @ResponseBody
    public NewsContent selectNewContentByNewsId(Long newsId){
        return newsPageService.selectNewContentByNewsId(newsId);
    }
}
